---
title: 'Read me for replication materials'
author: Maurits J. Meijers, Robert A. Huber, and Andrej Zaslove
output:
  rmdformats::downcute:
    use_bookdown: true
    self_contained: true
    thumbnails: false
    lightbox: true
    gallery: false
    highlight: monochrome
  editor_options:
  chunk_output_type: console
---

```{r globopt, include = F}
knitr::opts_chunk$set(warning = FALSE)
rm(list=ls())
```

This document guides through the replication for the following publication: Meijers, Maurits J., Robert A. Huber, and Andrej Zaslove 'The Anatomy of Populist Ideology: How Political Parties Define "The People" and "The Elite"' *European Journal of Political Research*. Thank you for your interest in our manuscript and the underlying data. 

# How to proceed

All steps required to replicate the results in `R` can be run from the file `r_code`.R`. This file includes all analyses with the POPPA data necessary to replicate the paper. If you want to learn more about the data generation process and recoding, please consult the replication materials for the POPPA data: <https://doi.org/10.7910/DVN/RMQREQ>.

# Prepare Replication Data

After downloading our replication material from <https://doi.org/10.7910/DVN/ECMQ4M>, please open the file `replication_material.Rproj`. You need to have installed `R` (version 4.3x) and `RStudio` for this. After opening the `.Rproj` file in `RStudio`, please open the file `r_code` within the project.

# Install/Load Packages

The following code installs all packages (if required). After that it loads the packages. The code then also uses the `here` package to set the working directory. Runtime of this code depends on the number of packages that you have already installed. However, even when you have no packages installed, it should only take a few minutes.

```{r pkgload, warning=FALSE, message=FALSE, results='hide'}
run_start_total <- Sys.time()
run_start <- Sys.time()

pkgs <- c("tidyverse",
          "marginaleffects",
          "texreg")

# Function to check if packages are installed
# If not: package will be installed from CRAN and then loaded
# If: Package will be loaed

install_load <- function(packages){
  
  for (p in packages) {
    cat("Check package: '", p, "'...\n", sep = "")
    flush.console()
    
    if (p %in% rownames(installed.packages())) {
      
      cat("Package: '", p, "' is already installed...\n\n", sep = "")
      flush.console()
      
      library(p, character.only=TRUE)
      
    } else {
      
      cat("Package: '", p, "' is NOT installed! Will install now...\n\n", sep = "")
      install.packages(p)
      library(p,character.only = TRUE)
      
    }
  }
  cat("\nAll packages installed!\n\n")
}

# Apply function to all required packages

install_load(pkgs)

# Set wd with here() package

here::i_am("replication_materials.Rproj")

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# List of initial files

The files should cover the following:

```{r}
#List of files
list.files()
```

# Analysis

This script `r_code.R`` runs all analyses shown in the paper and the appendix.

```{r, include=T, warning=FALSE, message=FALSE, results='hide', fig.show='hide'}
run_start <- Sys.time()

source("r_code.R", echo = T)

run_stop <- Sys.time()
run_time <- (run_stop - run_start)
run_time
```

# List of created files

The replication files should create the following:

```{r}
#List of files
list.files("./figures/")
list.files("./tables/")
```

# Session Info

This notebook was run using the following setup:

```{r}
pander::pander(sessionInfo())

run_stop_total <- Sys.time()
run_time_total <- (run_stop_total - run_start_total)
run_time_total
```
